﻿using BudgetData;
using BudgetSV.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BudgetSV.admin
{
    public partial class index : ManagePage
    {
        protected Manager u;
        public string MenuTopHtm = "";
        public string MenuHtm = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                u = GetAdminInfo();
                binddata();
            }
        }

        private void binddata()
        {
            var menus = new List<Navigation>();
            if (u.user_name == "admin")
            {
                menus = Mod.GetList<Navigation>("select * from Navigation where is_lock=0 order by sort_id");
            }
            else
            {
                menus = Mod.GetList<Navigation>("select * from Navigation WHERE is_lock=0 and name in (SELECT nav_name FROM ManagerRoleValue WHERE role_id in (SELECT role_id FROM ManagerRoleLK WHERE manager_id=0)) order by sort_id");
            }
            var baseMenus = menus.Where(c => c.parent_id == null).OrderBy(m => m.sort_id);
            MenuTopHtm = "";
            MenuHtm = "";
            foreach (var l in baseMenus)
            {
                MenuTopHtm += "<div class=\"list-group\" title=\"" + l.title + "\">" +
                        "<h1 title=\"" + l.title + "\">" +
                         "   <img src=\"/admin/skin/default/nav/sys.png\" /></h1>" +
                    "</div>";


                MenuHtm += "<div class=\"list-group\" title=\"" + l.title + "\">";
                MenuHtm += "<h1 title=\"" + l.title + "\">" +
                         "   <img src=\"/admin/skin/default/nav/sys.png\" /></h1>";

                //***c***//
                MenuHtm += "<div class=\"list-wrap\"><h2>" + l.title + "<i></i></h2>";
                MenuHtm += getchildhtm(l.id, menus);

                MenuHtm += "</div>";
                //***c***//

                MenuHtm += "</div>";

            }

        }

        private string getchildhtm(long id, List<Navigation> menus)
        {
            string csr = "";
            csr += "<ul>";
            var fMenus = menus.Where(c => c.parent_id == id).OrderBy(m => m.sort_id);
            foreach (var f in fMenus)
            {
                csr += "<li>";
                var hc = menus.Where(c => c.parent_id == f.id).OrderBy(m => m.sort_id).ToList();
                if (hc.Count > 0)
                {
                    csr += "<a navid=\"sys_manager\" target=\"mainframe\"><span>" + f.title + "</span></a>";
                    csr += getchildhtm(f.id, hc);
                }
                else {
                    csr += "<a navid=\"sys_manager\" href=\"" + f.link_url + "\" target=\"mainframe\"><span>" + f.title + "</span></a>";
                }
                csr += "</li>";

            }
            csr += "</ul>";
            return csr;
        }

        //安全退出
        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session[DTKeys.SESSION_ADMIN_INFO] = null;
            Utils.WriteCookie("AdminName", "Budget", -14400);
            Utils.WriteCookie("AdminPwd", "Budget", -14400);
            Response.Redirect("login.aspx");
        }

    }
}